<template>
  <div class="app-container">
    <!--表格渲染-->
    <el-table v-loading="loading" ref="table" :data="data" size="small" style="width: 100%;" border>
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="name" label="办理环节"/>
      <el-table-column prop="procDefName" label="流程名称"/>
      <el-table-column prop="version" label="流程版本"/>
      <el-table-column prop="vars.applyUserId" label="流程发起人"/>
      <el-table-column prop="createTime" label="完成时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="todoHandle(scope.row.processDefinitionId,scope.row.taskDefinitionKey,scope.row.vars.busId,scope.row.id,scope.row.processInstanceId,scope.row.name)">[详情]</el-button>
          <el-button size="mini" type="text" @click="history(scope.row.processDefinitionId,scope.row.executionId)">[跟踪]</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialog" title="流程详情" append-to-body width="85%">
      <img :src="flowImge">
    </el-dialog>
    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import { parseTime } from '@/utils/index'
import initData from '@/mixins/initData'
import { getImage } from '@/api/framework/flow/flowProcess'
import { getFormKey } from '@/api/framework/flow/oaFlow'

export default {
  mixins: [initData],
  data() {
    return {
      dialog: false,
      flowImge: ''
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    checkPermission,
    parseTime,
    beforeInit() {
      this.url = 'flowTask/historicData'
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      return true
    },
    history(processDefinitionId, id) {
      this.dialog = true
      getImage(processDefinitionId, id).then(res => {
        this.flowImge = 'data:image/png;base64,' + btoa(
          new Uint8Array(res)
            .reduce((data, byte) => data + String.fromCharCode(byte), '')
        )
      }).catch(err => {
        this.dialog = false
        console.log(err.response.data.message)
      })
    },
    todoHandle(procDefId, key, busId, taskId, processInstanceId, taskName) {
      getFormKey(procDefId, key).then(res => {
        this.$router.push({
          path: res,
          query: {
            busId: busId,
            taskDefKey: key,
            procInsId: processInstanceId,
            taskName: taskName

          }
        })
      }).catch(err => {
        console.log(err.response.data.message)
      })
    }
  }
}
</script>
